package com.zw.pdm.module.basic.service;

import com.zw.pdm.entity.basic.Dept;
import com.zw.pdm.entity.basic.Equ;
import com.zw.pdm.entity.basic.Person;
import com.zw.pdm.entity.basic.Process;
import com.zw.pdm.entity.basic.Route;
import com.zw.pdm.entity.sys.User;
import com.zw.pdm.module.basic.mapper.CraftDictionariesMapper;
import com.zw.pdm.module.basic.repository.*;
import com.zw.pdm.module.system.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import static com.zw.pdm.constant.Constant.STATUS_DELETE;
import static com.zw.pdm.util.BeanUtil.copy;

/**
 * @author mayongfeng
 * @version 1.0.0
 * @ClassName CraftDictionariesServer.java
 * @Description TODO
 * @createTime 2020年12月03日 08:56:00
 */
@Service
@SuppressWarnings("all")
public class CraftDictionariesServer {

    @Autowired
    private CraftDictionariesMapper craftDictionariesMapper;
    @Autowired
    private CraftComRouteRepository craftComRouteRepository;
    @Autowired
    private SysUserRepository sysUserRepository;
    @Autowired
    private SysDeptRepository sysDeptRepository;
    @Autowired
    private CraftComPersonRepository craftComPersonRepository;
    @Autowired
    private CraftComProcessRepository craftComProcessRepository;
    @Autowired
    private CraftComEquRepository craftComEquRepository;
    @Autowired
    private LogService logService;


    /**************************************************以下是工艺路线的数据字典配置****************************************/

    /**
     * @Author mayongfeng
     * @Date 8:57 2020/12/3
     * @Description 工艺路线字典查询
     *
     **/
    public List<Route> findAllCraftDiction(Route route) {

        return craftDictionariesMapper.findAllCraftDiction(route);
    }

    /**
     * @Author mayongfeng
     * @Date 8:57 2020/12/3
     * @Description 工艺路线字典数据修改
     *
     **/
    public Route updateRouteCraftDiction(Route route) {
        Route route1 = craftComRouteRepository.getOne(route.getId());
        copy(route,route1);
        Route updateRout = craftComRouteRepository.save(route1);
        logService.saveLog("修改工艺路线","名称："+updateRout.getName()+"，Id："+updateRout.getId());
        return updateRout;
    }

    /**
     * @Author mayongfeng
     * @Date 8:57 2020/12/3
     * @Description 工艺路线字典数据保存
     *
     *
     * @return*/
    public Route saveRouteCraftDiction(Route route) {
        /** 是否删除 1：是， 0：否 */
        route.setIsDel(STATUS_DELETE);
        /** 创建时间 */
        route.setCreateTime(new Date());
        Route save = craftComRouteRepository.save(route);
        logService.saveLog("添加工艺路线","名称："+save.getName()+"，Id："+save.getId());
        return save;
    }

    /**
     * @Author mayongfeng
     * @Date 8:57 2020/12/3
     * @Description 工艺路线字典数据删除
     *
     **/
    public Route delRouteCraftDiction(Route route) {
        Route route1 = craftComRouteRepository.getOne(route.getId());
        copy(route,route1);
        route1.setIsDel(1);
        Route delRoute = craftComRouteRepository.save(route1);
        logService.saveLog("删除工艺路线","名称："+delRoute.getName()+"，Id："+delRoute.getId());
        return delRoute;

    }

    /**************************************************以下是工艺审核人的数据字典配置**************************************/

    /**
     * @Author mayongfeng
     * @Date 8:57 2020/12/3
     * @Description 工艺审核人字典查询
     *
     **/
    public List<Person> findAllPersonCraftDiction(Person person) {
        return craftDictionariesMapper.findAllPersonCraftDiction(person);
    }


    /**
     * @Author mayongfeng
     * @Date 8:57 2020/12/3
     * @Description 通过员工编号查询员工信息
     *
     **/
    public User findAllPersonUserCraftDiction(String code) {
        return sysUserRepository.findByAccount(code);
    }

    /**
     * @Author mayongfeng
     * @Date 16:55 2020/12/3
     * @Description 查询部门
     *
     **/
    public List<Dept> selectDeptAll(Integer pid) {
        return sysDeptRepository.findByPid(pid);
    }

    /**
     * @Author mayongfeng
     * @Date 8:57 2020/12/3
     * @Description 工艺审核人字典数据保存
     *
     **/
    public Person savePersonCraftDiction(Person person) {
        /** 是否删除 */
        person.setIsDel(STATUS_DELETE);
        person.setCategory("工艺员");
        Person savePerson = craftComPersonRepository.save(person);
        logService.saveLog("保存工艺审核人","名称："+savePerson.getName()+"，Id："+savePerson.getId());
        return savePerson;


    }

    /**
     * @Author mayongfeng
     * @Date 8:57 2020/12/3
     * @Description 工艺审核人字典数据修改
     *
     **/
    public Person updatePersonCraftDiction(Person person) {
        Person person1 = craftComPersonRepository.getOne(person.getId());
        copy(person,person1);
        Person updatePerson = craftComPersonRepository.save(person1);
        logService.saveLog("修改工艺审核人","名称："+updatePerson.getName()+"，Id："+updatePerson.getId());
        return updatePerson;


    }

    /**
     * @Author mayongfeng
     * @Date 8:57 2020/12/3
     * @Description 工艺审核人字典数据删除
     *
     **/
    public Person delPersonCraftDiction(Person person) {
        Person delPerson = craftComPersonRepository.getOne(person.getId());
        delPerson.setIsDel(1);
        Person delP = craftComPersonRepository.save(delPerson);
        logService.saveLog("删除工艺审核人","名称："+delP.getName()+"，Id："+delP.getId());
        return delP;

    }

    /**************************************************以下是工序的数据字典配置*******************************************/

    /**
     * @Author mayongfeng
     * @Date 14:47 2020/12/7
     * @Description 工序查询
     *
     **/
    public List<Process> findProcessList(Process process) {
        return craftDictionariesMapper.findAllProcessList(process);
    }

    /**
     * @Author mayongfeng
     * @Date 14:47 2020/12/7
     * @Description 工序添加
     *
     **/
    public Process saveProcess(Process process) {
        process.setIsDel(0);
        process.setCreateTime(new Date());
        Process savePro = craftComProcessRepository.save(process);
        logService.saveLog("添加工序","名称："+savePro.getName()+"，Id："+savePro.getId());
        return savePro;
    }

    /**
     * @Author mayongfeng
     * @Date 15:05 2020/12/7
     * @Description 工序修改
     *
     **/
    public Process updatePorcess(Process process) {
        Process process1 = craftComProcessRepository.getOne(process.getId());
        copy(process,process1);
        Process modifyPorcess = craftComProcessRepository.save(process1);
        logService.saveLog("修改工序","名称："+modifyPorcess.getName()+"，Id："+modifyPorcess.getId());
        return modifyPorcess;
    }


    /**
     * @Author mayongfeng
     * @Date 15:57 2020/12/7
     * @Description 删除工序
     *
     **/
    public Process delProcess(Process process) {
        Process process1 = craftComProcessRepository.getOne(process.getId());
        process1.setIsDel(1);
        Process scDel = craftComProcessRepository.save(process1);
        logService.saveLog("删除工序","名称："+scDel.getName()+"，Id："+scDel.getId());
        return scDel;
    }

    /**************************************************以下是设备名称的数据字典配置****************************************/

    /**
     * @Author mayongfeng
     * @Date 14:47 2020/12/7
     * @Description 设备名称列表
     *
     **/
    public List<Equ> findAllEquList(Equ equ) {
        return craftDictionariesMapper.findAllEquList(equ);
    }

    /**
     * @Author mayongfeng
     * @Date 14:47 2020/12/7
     * @Description 设备名称添加
     *
     **/
    public Equ saveEqu(Equ equ) {
        equ.setIsDel(0);
        equ.setCreateTime(new Date());
        Equ addEqu = craftComEquRepository.save(equ);
        logService.saveLog("添加设备名称","名称："+addEqu.getName()+"，Id："+addEqu.getId());
        return addEqu;
    }

    /**
     * @Author mayongfeng
     * @Date 15:22 2020/12/7
     * @Description 修改设备名称
     *
     **/
    public Equ updateEqu(Equ equ) {
        Equ equ1 = craftComEquRepository.getOne(equ.getId());
        copy(equ,equ1);
        Equ modifyEqu = craftComEquRepository.save(equ1);
        logService.saveLog("修改设备名称","名称："+modifyEqu.getName()+"，Id："+modifyEqu.getId());
        return modifyEqu;
    }

    /**
     * @Author mayongfeng
     * @Date 16:00 2020/12/7
     * @Description 删除设备名称
     *
     **/
    public Equ delEqu(Equ equ) {
        Equ equ1 = craftComEquRepository.getOne(equ.getId());
        equ1.setIsDel(1);
        Equ outEqu = craftComEquRepository.save(equ1);
        logService.saveLog("删除设备名称","名称："+outEqu.getName()+"，Id："+outEqu.getId());
        return outEqu;
    }

}
